package com.niuma.controller;

import com.niuma.pojo.Books;
import com.niuma.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author niuma
 */
@Controller
@RequestMapping("/book")
public class BookController {

    // controller 调 service 层

    @Autowired
    @Qualifier("bookServiceImpl")
    BookService bookService;

    // 查询所有的书籍，并返回到一个书籍展示的页面

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list", list);
        return "allBook";
    }

    // 跳转到增加书籍页面

    @RequestMapping("/toAddBook")
    public String toAddPaper() {
        return "addBook";
    }

    // 添加书籍的请求

    @RequestMapping("/addBook")
    public String addBook(Books books) {
        System.out.println("addBook=====>" + books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    // 跳转到修改页面

    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model) {
        Books book = bookService.queryBookById(id);
        model.addAttribute("book", book);
        return "updateBook";
    }

    // 修改书籍

    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
        System.out.println("updateBook===>" + books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }

    // 删除书籍

    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
}
